const path = require('path');
const CompressionWebpackPlugin = require('compression-webpack-plugin');
const timeStamp = new Date().getTime();

function resolve(dir) {
    return path.join(__dirname, dir)
}

module.exports = {
    publicPath: process.env.NODE_ENV === 'production' ? '/admin/' : '/',
    productionSourceMap: false,
    devServer: {
        port: 8080,
        open: true,
        proxy: {
            '/api': {
                target: 'https://test2.xmobject.com',//代理地址
                changeOrigin: true,
                ws: true,
                pathRewrite: {
                    "^/api": ''
                }
            }
        }
    },
    chainWebpack: config => {
        config.optimization.splitChunks({
            chunks : 'all',
            cacheGroups : {
                elementUI : {
                    name: 'chunk-elementUI', // split elementUI into a single package
                    priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
                    test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
                },
            }
        })
    },
    configureWebpack: {
        resolve : {
            alias : {
                '@' : resolve('src'),
                '@c' : resolve('src/components'),
                '@ast' : resolve('src/assets'),
                '@css' : resolve('src/assets/css'),
                '@u' : resolve('src/utils'),
                '@v' : resolve('src/views'),
                '@const' : resolve('src/libs/constant'),
            },
        },
        
        //gzip压缩
        plugins: [
            new CompressionWebpackPlugin({
                filename: '[path].gz[query]',
                algorithm: 'gzip',
                test: /\.js$|\.html$|\.css/, //匹配文件名
                threshold: 10240,//对超过10k的数据压缩
                deleteOriginalAssets: false //不删除源文件
            }),
        ],
        //
        output: {
            filename: `js/[name].${timeStamp}.js`,
            chunkFilename: `js/[name].${timeStamp}.js`
        },
    }
}
